#include <iostream>
#include "controller.h"

Controller::Controller(void):region("1100"),terminal(0),macaddr(0),team(0)
{
	lastrecv = lastsend = lastbeat = time(0);
}
bool Controller::checkSendTimeout(void)
{
	if(abs(time(0) - lastsend) > 30)
	{
		return true;
	}
	return false;
}
bool Controller::checkRecvTimeout(void)
{
	int diff = abs(time(0) - lastrecv);
	if(diff > 600)
	{
		printf("%d seconds no recving\n", diff);
		return true;
	}
	return false;
}
bool Controller::doRecvWork(void)
{
	uint8_t c = 0;
	ACE_Time_Value t(0, 1000);

	if(recv_n(&c, 1, &t)==1)
	{
		recvbuf.push_back(c);
		lastrecv = time(0);
		//printf("%02X ", c);
		return true;
	}
	checkRecvTimeout();
	//perror("recv_n");
	return false;
}
bool Controller::doSendWork(void)
{
	if(sendbuf.length < 1)
	{
		return false;
	}
	if(send_n(sendbuf.data, sendbuf.length) == sendbuf.length)
	{
		lastsend = time(0);
		return true;
	}
	return false;
}
bool Controller::checkRecvBuffer(void)
{
	if(recvbuf.check() == true)
	{
		return true;
	}
	return false;
}
bool Controller::sendHeatbeat(void)
{
	if(abs(time(0) - lastbeat) > 10)
	{
		UserBuffer buf;

		lastbeat = time(0);
		if( buf.setRegion(region).
			setTerminal(terminal).
			setMac(macaddr).
			setTeam(false).
			setDir(MT_DIR_S2M).
			setPrm(MT_PRM_ACTIVE).
			setPos(MT_POS_SIGLE).
			setCon(false).
			setSeq(seq++).
			setNum(1).
			setApp(0).
			setPn(0).
			setAfn(AFN_02_LINK).
			setCmd(CMD_AFN_2_F3_HEART_BEAT).
		packup())
		{
			cout << "P: " << buf.toStringF() << endl;
			if(send_n(buf.data, buf.length)==buf.length)
			{
				cout << "T: " << buf.toStringF() << endl;
				return true;
			}
		}
	}
	return false;
}
bool Controller::confirmAll(void)
{
	UserBuffer buf;

	if( buf.setRegion	(region				).
		setTerminal		(terminal			).
		setMac			(recvbuf.getMac()	).
		setTeam			(team				).
		setDir			(MT_DIR_S2M			).
		setPrm			(MT_PRM_PASIVE		).
		setTpv			(false				).
		setPos			(MT_POS_SIGLE		).
		setCon			(false				).
		setNum			(1					).
		setApp			(0					).
		setPn			(0					).
		setAfn			(AFN_00_CONF		).
		setCmd			(CMD_AFN_0_F1_ALL_OK).
		setSeq			(recvbuf.getSeq()	).
	packup())
	{
		cout << __func__ << "P: " << buf.toStringF() << endl;
		if(send_n(buf.data, buf.length)==buf.length)
		{
			cout << __func__ << "T: " << buf.toStringF() << endl;
			return true;
		}
	}
	return false;
}
bool Controller::denyAll(void)
{
	UserBuffer buf;

	if( buf.setRegion	(region				).
		setTerminal		(terminal			).
		setMac			(recvbuf.getMac()	).
		setTeam			(team				).
		setDir			(MT_DIR_S2M			).
		setPrm			(MT_PRM_PASIVE		).
		setTpv			(false				).
		setPos			(MT_POS_SIGLE		).
		setCon			(false				).
		setNum			(1					).
		setApp			(0					).
		setPn			(0					).
		setAfn			(AFN_00_CONF		).
		setCmd			(CMD_AFN_0_F2_ALL_DENY).
		setSeq			(recvbuf.getSeq()	).
	packup())
	{
		cout << __func__ << "P: " << buf.toStringF() << endl;
		if(send_n(buf.data, buf.length)==buf.length)
		{
			cout << __func__ << "T: " << buf.toStringF() << endl;
			return true;
		}
	}
	return false;
}
bool Controller::confirmOneByOne(void)
{
	UserBuffer buf;

	if( buf.setRegion	(region				).
		setTerminal		(terminal			).
		setMac			(recvbuf.getMac()	).
		setTeam			(team				).
		setDir			(MT_DIR_S2M			).
		setPrm			(MT_PRM_PASIVE		).
		setTpv			(false				).
		setPos			(MT_POS_SIGLE		).
		setCon			(false				).
		setNum			(1					).
		setApp			(0					).
		setPn			(0					).
		setAfn			(AFN_00_CONF		).
		setCmd			(CMD_AFN_0_F3_ONE_BY_ONE).
		setSeq			(recvbuf.getSeq()	).
	packup())
	{
		cout << __func__ << "P: " << buf.toStringF() << endl;
		if(send_n(buf.data, buf.length)==buf.length)
		{
			cout << __func__ << "T: " << buf.toStringF() << endl;
			return true;
		}
	}
	return false;
}
